package com.qf.service.impl;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.domain.TbUser;
import com.qf.domain.UserLogin;
import com.qf.domain.UserSearch;
import com.qf.mapper.TbUserMapper;
import com.qf.service.TbUserService;
import com.qf.utils.JwtUtils;
import com.qf.utils.R;
import com.qf.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
* @author LC
* @description 针对表【tb_user】的数据库操作Service实现
* @createDate 2025-09-29 17:43:19
*/
@Service
public class TbUserServiceImpl implements TbUserService {
@Autowired
private TbUserMapper userMapper;
    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private RedisUtils redisUtil;
    @Override
    public R login(UserLogin user) {
        // 调用SpringSecurity的认证方法
        Authentication authentication = authenticationManager
                .authenticate(new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));
        Map<String, Object> claims = new HashMap<>();
        // 将用户名放入claims,用于存入jwt
        claims.put("username", authentication.getName());
        // 获取权限对象，用于提取权限数据
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        Set<String> authoritySet = new HashSet<>();
        authorities.forEach(item -> authoritySet.add(item.getAuthority()));
        // 将权限数据放入claims,用于存入jwt
        claims.put("perms", authoritySet);
        String s = UUID.randomUUID().toString();
        String uuid = s.replace("-", "");
        claims.put("uuid", uuid);
        String token = jwtUtils.createJwt(claims);
        redisUtil.setCacheObject("user:" + uuid, token, 15, TimeUnit.SECONDS);


        return R.ok(token);
    }

    @Override
    public R addUser(TbUser tbUser) {
        if(tbUser.getUsername()==null||tbUser.getPassword()==null){
            return R.error("用户名或密码不能为空");
        }
        int i = userMapper.addUser(tbUser);
        if(i>0){
            return R.ok();
        }
        return R.error("添加用户失败");
    }

    @Override
    public R getUserById(Integer id) {
        TbUser tbUser = userMapper.getUserById(id);
        if(tbUser!=null){
            return R.ok(tbUser);
        }
        return R.error("用户不存在");
    }

    @Override
    public R updateUserById(TbUser tbUser) {
        int i = userMapper.updateUserById(tbUser);
        if(i>0){
            return R.ok();
        }
        return R.error("修改用户失败");
    }

    @Override
    public R getAllUser() {
        if(userMapper.getAllUser()!=null){
            return R.ok(userMapper.getAllUser());
        }
        return R.error("没有用户");
    }

    @Override
    public R deleteUserById(Integer id) {
        int i = userMapper.deleteUserById(id);
        if(i>0){
            return R.ok();
        }
        return R.error("删除用户失败");
    }

    @Override
    public R deleteUsers(ArrayList<Integer> ids) {
        int i = userMapper.deleteUsers(ids);
        if(i>0){
            return R.ok();
        }
        return R.error("删除用户失败");
    }


    @Override
    public R getUserBySearch(UserSearch userSearch) {
        return R.ok(userMapper.getUserBySearch(userSearch));
    }
}
